package io.geekidea.springbootplus.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.annotations.CurrentAdmin;
import io.geekidea.springbootplus.system.annotations.CurrentAdminInfo;
import io.geekidea.springbootplus.system.entity.Co;
import io.geekidea.springbootplus.system.entity.ParcelCenterCo;
import io.geekidea.springbootplus.system.param.CoPageParam;
import io.geekidea.springbootplus.system.service.CoService;
import io.geekidea.springbootplus.system.service.ParcelCenterCoService;
import io.geekidea.springbootplus.system.vo.CoQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 控制器
 *
 * @author miko
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/co")
@Module("system")
@Api(value = "API", tags = {""})
public class CoControllerExtra extends BaseController {

    @Autowired
    private CoService coService;

    @Autowired
    private ParcelCenterCoService parcelCenterCoService;
    /**
     * 添加
     */
    @PostMapping("/new")
    @OperationLog(name = "添加", type = OperationLogType.ADD)
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<Boolean> addCo(@RequestBody JSONObject jsonObject) throws Exception {
        Co co = new Co();
        co.setCoName(jsonObject.getString("CoName"));
        co.setCreateTime(new Date());
        boolean flag = coService.saveCo(co);
        return ApiResult.result(flag);
    }

    /**
     * 添加
     */
    @PostMapping("/cgetAll")
    @OperationLog(name = "添加", type = OperationLogType.ADD)
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<List<Co>> cgetAll(@CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        List<Co> cos = coService.getAllList(new LambdaQueryWrapper<>());
        return ApiResult.ok(cos);
    }



    @PostMapping("/getAll")
    @OperationLog(name = "所有列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "所有列表", response = Co.class)
    public ApiResult<List<Co>> getAll(@CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        LambdaQueryWrapper<Co> wrapper = new LambdaQueryWrapper<>();
        List<Co> list = coService.getAllList(wrapper);

        List<ParcelCenterCo> l = parcelCenterCoService.list(new LambdaQueryWrapper<ParcelCenterCo>()
                .eq(ParcelCenterCo::getSchoolID, currentAdminInfo.getSchool().getSchoolID()));
        for (Co c : list) {
            l.forEach(item -> {
                if (c.getCoName().equals(item.getCoName())) {
                    c.setCenterName(item.getName());
                }
            });
            if(StringUtils.isEmpty(c.getCenterName())){
                c.setCenterName("其它");
            }
        }


        return ApiResult.ok(list);
    }

}
